package ifmo.group5512.timer.client;

import java.io.IOException;

public class Work {
	private ClientRunnable client;
	Thread mainThread;

	protected Work(String type) {
		setClient(new ClientRunnable("127.0.0.1", 18080, type));
		mainThread = new Thread(getClient());
		mainThread.start();

	}

	public void work() throws IOException {
		getClient().needWork(true);

		synchronized (getClient().monitor) {
			while (!getClient().client.canWork) {
				try {
					getClient().monitor.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		synchronized (getClient().monitor) {

			System.out.println(getClient().getTimerInput());
		}

		working();

		getClient().needWork(false);
		System.out.println("finished working");

	}

	public void stopThread() {
		synchronized (getClient().monitor) {
			getClient().exit = true;
		}
		mainThread.interrupt();
	}

	protected void working() {
		System.out.println("working");
	}

	public ClientRunnable getClient() {
		return client;
	}

	public void setClient(ClientRunnable client) {
		this.client = client;
	}
}
